import { defineSource } from '../source';
import { ProcessedItem } from '../types'
import { myFetch } from "../utils/fetch"
import logger from '../../framework/config/logger';

interface WapRes {
    code: number
    exp_str: string
    list: {
        hot_id: number
        keyword: string
        show_name: string
        score: number
        word_type: number
        goto_type: number
        goto_value: string
        icon: string
        live_id: any[]
        call_reason: number
        heat_layer: string
        pos: number
        id: number
        status: string
        name_type: string
        resource_id: number
        set_gray: number
        card_values: any[]
        heat_score: string
        stat_datas: {
            etime: string
            stime: string
            is_commercial: string
        }
    }[]
    top_list: any[]
    hotword_egg_info: string
    seid: string
    timestamp: number
    total_count: number
}

interface HotVideoRes {
    code: number
    message: string
    ttl: number
    data: {
        list: {
            aid: number
            videos: number
            tid: number
            tname: string
            copyright: number
            pic: string
            title: string
            pubdate: number
            ctime: number
            desc: string
            state: number
            duration: number
            owner: {
                mid: number
                name: string
                face: string
            }
            stat: {
                view: number
                danmaku: number
                reply: number
                favorite: number
                coin: number
                share: number
                now_rank: number
                his_rank: number
                like: number
                dislike: number
            }
            dynamic: string
            cid: number
            dimension: {
                width: number
                height: number
                rotate: number
            }
            short_link: string
            short_link_v2: string
            bvid: string
            rcmd_reason: {
                content: string
                corner_mark: number
            }
        }[]
    }
}


/**
 * 热搜榜
 */
// export default defineSource({
//     bilibili: async (): Promise<ProcessedItem[]> => {
//         const url = "https://s.search.bilibili.com/main/hotword?limit=30"
//         const res: WapRes = await myFetch(url)

//         return res.list.map(k => ({
//             id: k.keyword,
//             title: k.show_name,
//             url: `https://search.bilibili.com/all?keyword=${encodeURIComponent(k.keyword)}`,
//             extra: {
//                 hot: k.heat_score,
//             },
//         }))
//     }
// });

/**
 * 排行榜 
 */
export default defineSource({
    bilibili: async (): Promise<ProcessedItem[]> => {
        const url = "https://api.bilibili.com/x/web-interface/ranking/v2?rid=0&type=all"
        const res: HotVideoRes = await myFetch(url)
        return res?.data?.list.map(video => (
            {
            id: video?.bvid,
            title: video?.title,
            url: `https://www.bilibili.com/video/${video.bvid}`,
            extra: {
                hot: `${video?.stat?.view}观看 · ${video?.stat?.like}点赞`,
                content: video?.desc,
            },
        }))
    }
});
